#include <iostream>
#include <vector>
#include <cmath>
#include <random>

// 生成模拟信号
std::vector<double> generateAnalogSignal(double frequency, double duration, double samplingRate) {
    std::vector<double> signal;
    double angularFrequency = 2 * M_PI * frequency;
    for (double t = 0; t < duration; t += 1 / samplingRate) {
        signal.push_back(sin(angularFrequency * t));
    }
    return signal;
}

// 采样函数
std::vector<double> sampleSignal(const std::vector<double>& analogSignal, double samplingRate) {
    std::vector<double> sampledSignal;
    double period = 1 / samplingRate;
    for (size_t i = 0; i < analogSignal.size(); i += static_cast<size_t>(analogSignal.size() * period)) {
        sampledSignal.push_back(analogSignal[i]);
    }
    return sampledSignal;
}

// 重建信号（简单线性插值）
std::vector<double> reconstructSignal(const std::vector<double>& sampledSignal, double originalSamplingRate, double newSamplingRate) {
    std::vector<double> reconstructedSignal;
    double ratio = originalSamplingRate / newSamplingRate;
    for (size_t i = 0; i < sampledSignal.size() - 1; ++i) {
        for (int j = 0; j < ratio; ++j) {
            double t = static_cast<double>(j) / ratio;
            double interpolatedValue = sampledSignal[i] * (1 - t) + sampledSignal[i + 1] * t;
            reconstructedSignal.push_back(interpolatedValue);
        }
    }
    return reconstructedSignal;
}

// 计算均方误差（MSE）评估重建质量
double calculateMSE(const std::vector<double>& originalSignal, const std::vector<double>& reconstructedSignal) {
    double mse = 0.0;
    for (size_t i = 0; i < originalSignal.size() && i < reconstructedSignal.size(); ++i) {
        double diff = originalSignal[i] - reconstructedSignal[i];
        mse += diff * diff;
    }
    mse /= static_cast<double>(originalSignal.size());
    return mse;
}

int main() {
    double frequency = 10.0; // 模拟信号频率
    double duration = 1.0; // 信号持续时间
    double originalSamplingRate = 100.0; // 原始采样率（大于两倍信号频率）
    double newSamplingRate1 = 15.0; // 新采样率（小于两倍信号频率）
    double newSamplingRate2 = 25.0; // 新采样率（大于两倍信号频率）

    // 生成模拟信号
    std::vector<double> analogSignal = generateAnalogSignal(frequency, duration, originalSamplingRate);

    // 以不同采样率采样
    std::vector<double> sampledSignal1 = sampleSignal(analogSignal, newSamplingRate1);
    std::vector<double> sampledSignal2 = sampleSignal(analogSignal, newSamplingRate2);

    // 重建信号
    std::vector<double> reconstructedSignal1 = reconstructSignal(sampledSignal1, originalSamplingRate, newSamplingRate1);
    std::vector<double> reconstructedSignal2 = reconstructSignal(sampledSignal2, originalSamplingRate, newSamplingRate2);

    // 计算均方误差
    double mse1 = calculateMSE(analogSignal, reconstructedSignal1);
    double mse2 = calculateMSE(analogSignal, reconstructedSignal2);

    std::cout << "采样率小于两倍信号频率时的均方误差: " << mse1 << std::endl;
    std::cout << "采样率大于两倍信号频率时的均方误差: " << mse2 << std::endl;

    if (mse1 > mse2) {
        std::cout << "验证成功：采样率小于两倍最高有效频率时重建误差更大，无法有效重建信号。" << std::endl;
    } else {
        std::cout << "验证失败：采样率小于两倍最高有效频率时重建误差不大于采样率大于两倍最高有效频率时的误差。" << std::endl;
    }

    return 0;
}